package controlFlowGraph;

import java.util.HashSet;
import java.util.Set;

public class LabStore extends CFGLabel {

	private Expression leftSide;
	
	private Expression rightSide;
	
	public LabStore(Expression l, Expression r) {
		this.leftSide = l;
		this.rightSide = r;
	}
	
	@Override
	public Set<Expression> getInvolvedExpressions() {
		Set<Expression> res = new HashSet<Expression>();
		res.add(rightSide);
		res.add(leftSide);
		return res;
	}

	@Override
	public String toString() {
		return "M["+this.leftSide+"] = " + this.rightSide;
	}

	public Expression getRightSide() {
		return this.rightSide;
	}

	public Expression getLeftSide() {
		return this.leftSide;
	}

	public void setRightSide(Variable variable) {
		this.rightSide = variable;
	}

	public void setLeftSide(Variable variable) {
		this.leftSide = variable;
	}
	
	@Override
	public void replaceVariable(Variable v, Constant c){
		if(this.rightSide instanceof Variable){
			if(((Variable) this.rightSide).equals(v)){
				this.rightSide = c;
			}
		} else {
			this.rightSide.replaceVariable(v,c);
		}
		if(this.leftSide instanceof Variable){
			if(((Variable) this.leftSide).equals(v)){
				this.leftSide = c;
			}
		} else {
			this.leftSide.replaceVariable(v,c);
		}
	}
}
